#include "head_client.h"

int transFile(int netfd, char *path, char *fileNameToUser)
{
    // // train_t train = {5,"file1"};
    // // 发文件名file1
    // long len = strlen(fileNameToUser);
    // printf("strlen(fileNameToUser) = %ld\n", strlen(fileNameToUser));
    train_t train;
    // bzero(&train, sizeof(train));
    // train.length = strlen(fileNameToUser);
    // strcpy(train.data, fileNameToUser);
    // send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

    // file1
    int fd = open(fileNameToUser, O_RDONLY);
    struct stat statbuf;
    fstat(fd, &statbuf);
    printf("filesize = %ld\n", statbuf.st_size);
    train.length = sizeof(statbuf.st_size);
    memcpy(train.data, &statbuf.st_size, train.length);
    send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    while (1)
    {
        ssize_t sret = read(fd, train.data, sizeof(train.data));
        if (sret == 0)
        {
            break;
        }
        train.length = sret;
        sret = send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        if (train.length != 1000)
        {
            printf("train.length = %d\n", train.length);
        }
    }
    train.length = 0;
    send(netfd, &train, sizeof(train.length), MSG_NOSIGNAL);
    close(fd);

    int fd2 = open(fileNameToUser,O_RDONLY);
    md5(fd2,"unuse");
    return 0;
}